<!DOCTYPE html>
<html>
<!--
Copyright 2009 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by an Apache 2.0 License.
See the COPYING file for details.
-->
<!--
  Author: goto@google.com (sam Goto)
-->
<head>
<title>Closure Unit Tests - goog.ui.media.Picasa</title>
<script src="../../base.js"></script>
<script>
  goog.require('goog.debug.Logger');
  goog.require('goog.dom');
  goog.require('goog.events');
  goog.require('goog.testing.jsunit');
  goog.require('goog.testing.LooseMock');
  goog.require('goog.ui.Component');
  goog.require('goog.ui.Control');
  goog.require('goog.ui.ControlRenderer');
  goog.require('goog.ui.media.FlashObject');
  goog.require('goog.ui.media.PicasaAlbum');
  goog.require('goog.ui.media.PicasaAlbumModel');
</script>
</head>
<body>
<script>

  var picasa;
  var control;
  var PICASA_USERNAME = 'username';
  var PICASA_ALBUM = 'albumname';
  var PICASA_URL = 'http://picasaweb.google.com/' + PICASA_USERNAME + '/' +
      PICASA_ALBUM;
  
  function setUp() {
    picasa = new goog.ui.media.PicasaAlbum();
    var model = new goog.ui.media.PicasaAlbumModel(PICASA_USERNAME,
        PICASA_ALBUM, null, 'album title');
    control = new goog.ui.media.Media(model, picasa);
    control.setSelected(true);    
  }

  function tearDown() {
    control.dispose();
  }

  function testBasicRendering() {
    control.render();
    var el = goog.dom.getElementsByTagNameAndClass('div',
        goog.ui.media.PicasaAlbum.CSS_CLASS);
    assertEquals(1, el.length);
  }

  function testParsingUrl() {
    var metadata = goog.ui.media.PicasaAlbumModel.newInstance(PICASA_URL);

    var expected = new goog.ui.media.PicasaAlbumModel(PICASA_USERNAME,
        PICASA_ALBUM);
    var model = goog.ui.media.PicasaAlbumModel.newInstance(PICASA_URL); 
    assertEquals(expected.getUserId(), model.getUserId());
    assertEquals(expected.getAlbumId(), model.getAlbumId());

    expected = new goog.ui.media.PicasaAlbumModel('foo', 'bar');
    model = goog.ui.media.PicasaAlbumModel.newInstance(
        'https://picasaweb.google.com/foo/bar'); 
    assertEquals(expected.getUserId(), model.getUserId());
    assertEquals(expected.getAlbumId(), model.getAlbumId());

    expected = new goog.ui.media.PicasaAlbumModel('foo', 'bar');
    model = goog.ui.media.PicasaAlbumModel.newInstance(
        'https://www.picasaweb.google.com/foo/bar'); 
    assertEquals(expected.getUserId(), model.getUserId());
    assertEquals(expected.getAlbumId(), model.getAlbumId());

    expected = new goog.ui.media.PicasaAlbumModel('foo', 'bar');
    model = goog.ui.media.PicasaAlbumModel.newInstance(
        'https://www.picasaweb.com/foo/bar'); 
    assertEquals(expected.getUserId(), model.getUserId());
    assertEquals(expected.getAlbumId(), model.getAlbumId());

    expected = new goog.ui.media.PicasaAlbumModel('foo', 'bar', '8Hzg1CUUAZM');
    model = goog.ui.media.PicasaAlbumModel.newInstance(
        'https://www.picasaweb.com/foo/bar' +
        '?authkey=8Hzg1CUUAZM#'); 
    assertEquals(expected.getUserId(), model.getUserId());
    assertEquals(expected.getAlbumId(), model.getAlbumId());
    assertEquals(expected.getAuthKey(), model.getAuthKey());

    expected = new goog.ui.media.PicasaAlbumModel('foo', 'bar', '8Hzg1CUUAZM');
    model = goog.ui.media.PicasaAlbumModel.newInstance(
        'https://www.picasaweb.com/foo/bar' +
        '?foo=bar&authkey=8Hzg1CUUAZM#'); 
    assertEquals(expected.getUserId(), model.getUserId());
    assertEquals(expected.getAlbumId(), model.getAlbumId());
    assertEquals(expected.getAuthKey(), model.getAuthKey());

    expected = new goog.ui.media.PicasaAlbumModel('foo', 'bar', '8Hzg1CUUAZM');
    model = goog.ui.media.PicasaAlbumModel.newInstance(
        'https://www.picasaweb.com/foo/bar' +
        '?foo=bar&authkey=8Hzg1CUUAZM&hello=world#'); 

    assertThrows('invalid urls parsing should fail', function() {
      goog.ui.media.PicasaAlbumModel.newInstance(
          'http://invalidUrl/watch?v=dMH0bHeiRNg');
    });
  }

  function testBuildingUrl() {
    assertEquals(PICASA_URL,
        goog.ui.media.PicasaAlbumModel.buildUrl(PICASA_USERNAME, PICASA_ALBUM));
  }

  function testCreatingDomOnInitialState() {
    control.render();

    var caption = goog.dom.getElementsByTagNameAndClass(
        'div',
        goog.ui.media.PicasaAlbum.CSS_CLASS + '-caption');
    assertEquals(1, caption.length);

    var flash = goog.dom.getElementsByTagNameAndClass('div',
        goog.ui.media.FlashObject.CSS_CLASS);
    assertEquals(1, flash.length);
  }
</script>
</body>
</html>
